#include "MainMenu/MainMenu.h"
#include <iostream>

#if INSTALANDO
#define PATH_FONT PATH_RESOURCES
#else
#define PATH_FONT "../Resources/"
#endif

SDL_Surface* CrearPantalla()
{

    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Surface* pantalla = SDL_SetVideoMode( 650, 500, 32, SDL_SWSURFACE | SDL_RESIZABLE);

    SDL_WM_SetCaption( "Menu - Biza Kong", NULL ); //nombre pedoooooorrooo

    TTF_Init();
    
    Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 );

    return pantalla;
}

int main()
{	
	LOG_START;
	
	SDL_Surface* pantalla = CrearPantalla();
	string yamlfile = SINCRONIZADOR_DATA_PATH;
	yamlfile += "juego.yaml";
	
	while (true) {
		
		pantalla = SDL_SetVideoMode( 650, 500, 32, SDL_HWSURFACE | SDL_RESIZABLE);

		MainMenu* menu_principal = new MainMenu(yamlfile);
		
		menu_principal->Mostrar(pantalla);	

		if (menu_principal->getCliente()) {
			if (menu_principal->getCliente()->getEstadoConexion() != Inactiva) {

				menu_principal->getCliente()->setVista((menu_principal->getJuego()).pantalla.ancho, (menu_principal->getJuego()).pantalla.alto, menu_principal->getCliente()->getMargenScroll(), menu_principal->getAnchoNivel(), menu_principal->getAltoNivel());
				menu_principal->getCliente()->setImagenFondo(0);
				int personaje = menu_principal->getInfoPersonajes().at(menu_principal->getMenu()->getSolapas().at(0)->getDeslizantes().at(0)->getSeleccionado())->id;
				menu_principal->getCliente()->Comenzar(menu_principal->getInfoImagenes(), menu_principal->getInfoSonidos(), personaje);
			
				if( Mix_PlayingMusic() == 1 )
				{
					Mix_HaltMusic();
				}
				Mix_FreeMusic( menu_principal->getMusica() );
			
				
				menu_principal->getCliente()->Jugar();
				delete menu_principal->getCliente();
				if (menu_principal->getModo() == 0 ) {
					delete menu_principal->getServidor();
				}
			}

		}

	    if ( menu_principal->getModo() == 1 ) {
			if (menu_principal->getServidor()) {

				SDL_Surface* fondo = SDL_CreateRGBSurface(0, 650, 500, 32, 255, 255, 255, 0 );
				SDL_FillRect(fondo, NULL, SDL_MapRGB(fondo->format, 0, 0, 0));
				string path_font = PATH_FONT;
				path_font += "fontMario.ttf";
				TTF_Font* font40 = TTF_OpenFont(path_font.c_str(), 40 );
				SDL_Color white = {255, 255, 255};
				SDL_Surface* texto =  TTF_RenderText_Solid( font40, "SERVIDOR CORRIENDO" , white);
            
				insertar_imagen(100,200,texto,fondo);
				insertar_imagen(0,0,fondo,pantalla);
				SDL_Flip(pantalla);
			
				menu_principal->getServidor()->Correr();
			
				delete menu_principal->getServidor();
			}
		}

		delete menu_principal;

	}
    return 0;
}
